import Vue from 'vue'
import Router from 'vue-router'
import DynamicRouter from "../utils/DynamicRouter";



Vue.use(Router);

var baseRouters = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/login',
    component: () => import("../views/Login")
  },
  {
    path: '/home',
    component: () => import("../views/Home"),
    children: [
    ]
  },
  {
    path: '/main',
    component: () => import("../views/main")
  }
];

let notFound = {
  path: "*",
  component: () => import("../views/NotFoundPage")
};

const router = new Router({
  routes: baseRouters,
  mode: 'history'
})
//登陆后只执行一次add方法 采用加锁的形式进行验证
let off = true;
router.beforeEach((to, from, next) => {
  if (off) {
    let permissionVos = add();
    baseRouters.push(notFound);
    if (permissionVos.length > 0) {
      off = false;
    }
  }
  next();
})



export function add() {
  let data = DynamicRouter.methods.getSysPermissionVos();
  let handelDatas = routesData(data);
  handelDatas.forEach(item => {
    baseRouters[2].children.push(item);
  })
  router.addRoutes(baseRouters);
  return data;

}


function routesData(data) {
  let permissionData = [];
  data.forEach(item => {
    if(item.children){
      let subChildren = [];
      item.children.forEach(subItem =>{
        if (subItem.children){
          let subSubChildren = [];
          subItem.children.forEach(subSubItem =>{
            let subSubData = {
              path: subSubItem.path,
              component: () => import("../views/" + subSubItem.component)
            };
            subSubChildren.push(subSubData);
          })
          subItem.children = subSubChildren;
        }
        let subData = {
          path: subItem.path,
          component : () => import("../views/" + subItem.component),
          children: subItem.children
        };
        subChildren.push(subData);
      })
      item.children = subChildren;
    }
    let topData = {
      path: item.path,
      component: () => import("../views/" + item.component),
      children: item.children
    };
    permissionData.push(topData);
  })
  return permissionData;
}

// const originalPush = Router.prototype.push
// const originalReplace = Router.prototype.replace
// // push
// Router.prototype.push = function push (location, onResolve, onReject) {
//   if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
//   return originalPush.call(this, location).catch(err => err)
// }
// // replace
// Router.prototype.replace = function push (location, onResolve, onReject) {
//   if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject)
//   return originalReplace.call(this, location).catch(err => err)
// }

export default router;



// data.forEach(item => {
//   if(item.children){
//     let subChilderns = [];
//     item.children.forEach(subItem => {
//       if (subItem.children){
//         let subSubChildrens = [];
//         subItem.children.forEach(subSubItem => {
//           let data = {
//             path: subSubItem.path,
//             component: () => import("../views/" + subSubItem.component)
//           };
//           subSubChildrens.push(data);
//         })
//         subItem.children = subSubChildrens;
//         let data = {
//           path: subItem.path,
//           component: () => import("../views/" + subItem.component),
//           children: subItem.children
//         }
//         subChilderns.push(data);
//       }
//     })
//     item.children = subChilderns;
//     let data = {
//       path: item.path,
//       component: () => import("../views/" + item.component),
//       children: item.children
//     }
//     permissionData.push(data);
//   }
// })
